package dao.impl;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import dao.QuestionDao;

import domain.Answer;
import domain.Question;

public class QuestionDaoImpl implements QuestionDao {

	@SuppressWarnings("unchecked")
	public List<Question> list() {
		Session session = HibernateSessionFactory.getSession();
		List<Question> list;
		list = session.createQuery("from Question").list();
		if (session != null)
			session.close();

		return list;
	}

	public Question findById(int Id) {
		try {
			Session session = HibernateSessionFactory.getSession();
			Question question = (Question) session.get(Question.class, Id);
			if (session != null)
				session.close();
			return question;
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void save(Question question) {
		try {
			Session session;
			session = null;
			Transaction tx = null;
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			session.save(question);
			session.flush();
			tx.commit();
			if (session != null)
				session.close();

		} catch (HibernateException e) {

			e.printStackTrace();
		}

	}

	public void update(Question question) {
		Session session = HibernateSessionFactory.getSession();
		Transaction transaction = session.getTransaction();
		transaction.begin();
		session.merge(question);
		transaction.commit();
		if (session != null)
			session.close();
	}

	public void delete(int id) {
		Session session = HibernateSessionFactory.getSession();
		Transaction transaction =session.getTransaction();
		transaction.begin();
		session.delete(session.get(Question.class, id));
		transaction.commit();
		if (session != null)
			session.close();
	}

	public Set<Answer> getAnswerSet(int questionId) {
				if (this.findById(questionId)!= null) {
			Session session = HibernateSessionFactory.getSession();
			Question questionTemp = (Question) session.load(Question.class, questionId);
			Set<Answer> answerSet = null;
			if(questionTemp.getAnswers()!=null) 
				answerSet=questionTemp.getAnswers();
	
			if (session != null)
				session.close();
			return answerSet;
		}
		return null;
	}

	public Iterator<Answer> getAnswerIterator(int questionId) {
		
		if (this.findById(questionId)!= null) {
			Session session = HibernateSessionFactory.getSession();
			Question questionTemp = null;
			questionTemp=(Question) session.load(Question.class, questionId);
			Set<Answer> answerSet = new HashSet<Answer>();
			if (questionTemp.getAnswers() != null)
				answerSet = questionTemp.getAnswers();
			System.out.print("out getAnswers()");
			Iterator<Answer> iter = null;
			if (answerSet.size() > 0)
				iter = answerSet.iterator();
			if (session != null)
				session.close();
			return iter;
		}
		return null;
	}
}
